//
//  BSMyInfoView.m
//  SuoSiApp
//
//  Created by zhang on 16/4/9.
//  Copyright © 2016年 Ym. All rights reserved.
//

#import "BSMyInfoView.h"

@interface BSMyInfoView ()<UITextFieldDelegate>
//@property (nonatomic, assign) BOOL IschangekeyBoard;
//@property (nonatomic, assign) CGFloat keyboardOffset;
@end

@implementation BSMyInfoView


- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview1];
    }
    return self;
}
- (void)addSubview1{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"(注: *号为必填项目)"];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,1)];
    
   // [super addSubview];
//    我的信息
    CGFloat Y = self.navigationView.bottom;
//    self.myInfoLB = [[UILabel alloc]initWithFrame:CGRectMake(kMartin20, Y , kMartin100, 43)];
    self.myInfoLB = [[UILabel alloc]initWithFrame:CGRectMake(kMartin20, 0 , kMartin100, 43)];

    self.myInfoLB.text = @"我的信息";
//    self.myInfoLB.attributedText = str;
    self.myInfoLB.font = [UIFont systemFontOfSize:24];
//    [self addSubview:_myInfoLB];
    [self.scrollView addSubview:_myInfoLB];
//    提示
    self.noteBT = [[UILabel alloc]initWithFrame:CGRectMake(self.myInfoLB.right, kMartin15, kScrenWidth - kMartin20 - self.myInfoLB.right, (43 - kMartin5)/2)];
    self.noteBT.attributedText = str;
//    self.noteBT.backgroundColor = BSWhite;
//    [self addSubview:_noteBT];
    [self.scrollView addSubview:_noteBT];
    
    Y = self.myInfoLB.bottom;
//    self.deviceIDLB = [self makeLableWithFrameY:Y text:<#(NSString *)#>];
    
//    self.deviceIDLB = [UILabel alloc]initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
   self.deviceIDLB = [self makeLableWithFrameY:Y text:@" 设备号*"];
    [self makeLableWithFrameY:Y + 44 text:@" 姓名*"];
    [self makeLableWithFrameY:Y + 88 text:@" 身份证*"];
    [self makeLableWithFrameY:Y + 44 * 3 text:@" 身高*"];
    [self makeLableWithFrameY:Y + 44 * 4 text:@" 体重*"];
    
    
    Y = Y + 44 * 5 + 6;
    
    self.phoneLB = [self makeLableWithFrameY:Y text:@" 验证手机*"];
    [self getLableWithFrameY:Y + 44 text:@" 家庭住址"];
    [self getLableWithFrameY:Y  + 88 text:@" 邮编"];
    self.remarkLB =  [self getLableWithFrameY:Y + 44 * 3 text:@" 备注事项"];
    self.scrollView.contentSize =CGSizeMake(kScrenWidth, self.remarkLB.bottom + kMartin20);
    
    
//    可以输入的内容
    CGFloat YTF = self.deviceIDLB.top;
    self.deviceIDTF = [self makeTextfeildWithFrameY:YTF placeHolder:@"设备号"];
    self.deviceIDTF.tag = 506;
    self.nameTF = [self makeTextfeildWithFrameY:YTF + 44 placeHolder:@"请输入姓名"];
    self.nameTF.tag = 506;
    self.IDCardTF = [self makeTextfeildWithFrameY:YTF + 44 * 2 placeHolder:@"请输入身份证号"];
    self.IDCardTF.tag = 506;
    self.heightTF = [self makeTextfeildWithFrameY:YTF + 44 * 3 placeHolder:@"请输入您的身高cm"];
    self.heightTF.clearsOnBeginEditing = YES;
    self.heightTF.tag = 501;
    self.weightTF = [self makeTextfeildWithFrameY:YTF + 44 * 4 placeHolder:@"请输入您的体重Kg"];
    self.weightTF.clearsOnBeginEditing = YES;

    self.weightTF.tag = 501;
    
//    姓名 性别 年龄
    self.nameTF.width = kScrenWidth *5/8 - kMartin50 - 20;
    self.sexLable = [[UILabel alloc]initWithFrame:CGRectMake(self.nameTF.right, self.nameTF.top, kMartin30, self.nameTF.height)];
    self.sexLable.backgroundColor = BSWhite;
    [self.scrollView addSubview:_sexLable];
    
    self.ageLable = [[UILabel alloc]initWithFrame:CGRectMake(self.sexLable.right -1, self.sexLable.top, kMartin40, self.nameTF.height)];
    self.ageLable.backgroundColor = BSWhite;
    [self.scrollView addSubview:_ageLable];
    
    
    
    
//    验证手机
    YTF = self.phoneLB.top  ;
    self.phoneTF = [self makeTextfeildWithFrameY:YTF placeHolder:@"验证手机"];
    self.phoneTF.tag = 500;
    self.addressTF = [self makeTextfeildWithFrameY:YTF + 44 placeHolder:@"请输入的家庭住址"];
    self.addressTF.tag = 506;
    self.postCodeTF = [self makeTextfeildWithFrameY:YTF + 88 placeHolder:@"请输入邮编"];
    self.postCodeTF.tag = 501;
    self.remarkTF = [self makeTextfeildWithFrameY:YTF + 44 * 3 placeHolder:@"建议输入过往重大病史"];
    self.remarkTF.tag = 506;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UILabel *)makeLableWithFrameY:(CGFloat)Y text:(NSString *)text{

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:text];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(text.length - 1,1)];
    
    CGRect frame = CGRectMake(0, Y, kScrenWidth*3/8, 43);
    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
    lable.attributedText = str;
    lable.backgroundColor = BSWhite;
    lable.font = [UIFont systemFontOfSize:20];
    [self.scrollView addSubview:lable];
    return lable;
}
// 必填项目
- (NSAttributedString *)getAttributeStringWithString:(NSString*)text{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:text];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(text.length - 1,1)];
    return str;
}

- (UILabel *)getLableWithFrameY:(CGFloat)Y text:(NSString *)text{
    
      CGRect frame = CGRectMake(0, Y, kScrenWidth*3/8 , 43);
    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
    lable.text = text;
    lable.font = [UIFont systemFontOfSize:20];
    lable.backgroundColor = BSWhite;
    [self.scrollView addSubview:lable];
    return lable;
}


- (UITextField *)makeTextfeildWithFrameY:(CGFloat)YTF placeHolder:(NSString *)text{

    UITextField *textFeild = [[UITextField alloc]initWithFrame:CGRectMake(kScrenWidth*3/8 - 1, YTF, kScrenWidth *5/8 , 43)];
    textFeild.delegate = self;;
    textFeild.placeholder = text;
    textFeild.backgroundColor = BSWhite;

    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(textFeild.right - kMartin10, textFeild.top, kMartin10, textFeild.height)];
    lable.backgroundColor = BSWhite;
    textFeild.rightViewMode = UITextFieldViewModeAlways;

    textFeild.rightView = lable;
    
    textFeild.textAlignment = NSTextAlignmentRight;
    textFeild.font = [UIFont systemFontOfSize:20];
    textFeild.borderStyle = UITextBorderStyleNone;
    [self.scrollView addSubview:textFeild];
    return textFeild;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
  
    return YES;
}

- (void)setInfoBaseModel:(BSUIUserInfoBase *)infoBaseModel{
    _infoBaseModel = infoBaseModel;
    
    if (infoBaseModel.RealName.length > 0) {
        self.nameTF.text = infoBaseModel.RealName;
        self.nameTF.userInteractionEnabled = NO;
    } else {
        ;
    }
    
    if (infoBaseModel.Sex == 1) {
            self.sexLable.text = [NSString stringWithFormat:@",%@",@"男"];
    } else {
            self.sexLable.text = [NSString stringWithFormat:@",%@",@"女"];
    }
    self.sexLable.font = [UIFont systemFontOfSize:20];
    self.ageLable.text = [NSString stringWithFormat:@",%ld",(long)infoBaseModel.Age];
    self.ageLable.font = [UIFont systemFontOfSize:20];
    self.IDCardTF.text = infoBaseModel.IDCard;
    self.IDCardTF.userInteractionEnabled = NO;
    
    
    if (infoBaseModel.Height != nil) {
        self.heightTF.text = [NSString stringWithFormat:@"%@cm",infoBaseModel.Height];

    }
    if (infoBaseModel.Weight != nil) {
        self.weightTF.text = [NSString stringWithFormat:@"%@Kg",infoBaseModel.Weight];

    }
 if([BSBluetoothSingleton shareBlutooth].uuiduuid.length > 20 && [BSBluetoothSingleton shareBlutooth].uuiduuid) {
        
        
        NSString *subUUID = [[BSBluetoothSingleton shareBlutooth].uuiduuid substringWithRange:NSMakeRange([[BSBluetoothSingleton shareBlutooth].uuiduuid length]-22, 4)];
        NSString *number = [self convertStringToInt:subUUID];
        
        NSString * peripheralsName = [NSString stringWithFormat:@"BC%@",number];
        self.deviceIDTF.text = peripheralsName;
        
    }


    self.deviceIDTF.userInteractionEnabled = NO;
    self.addressTF.text = infoBaseModel.FullAddr;
    self.postCodeTF.text = infoBaseModel.ZipCode;
    self.remarkTF.text = infoBaseModel.Remarks;
   // self.phoneTF.text = infoBaseModel.
    

    


//    @property(nonatomic, assign) NSInteger Sex;
//    @property(nonatomic, assign) NSInteger Age;
}



//- (void)textFieldDidEndEditing:(UITextField *)textField{
//
//    
//    if (self.IschangekeyBoard == YES) {
//        CGFloat duration = 0.3;
//        [UIView animateWithDuration:duration animations:^{
//            
//            self.transform = CGAffineTransformTranslate(self.transform, 0, self.keyboardOffset);
//            self.IschangekeyBoard = NO;
//            
//        }];
//    }
//    
//
//
//
//}
//
//- (void)textFieldDidBeginEditing:(UITextField *)textField{
//
//    
//    
//    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
//  CGPoint pp = [self convertPoint:CGPointMake(0, textField.bottom) toView:currentWindow];
//    
//    
//    self.keyboardOffset = self.navigationView.bottom + pp.y + 253 - kScrenHeight;
//    
//    if (253 + pp.y + self.navigationView.bottom > kScrenHeight ) {
//        NSLog(@"%f",self.keybordHeight);
//        
//        CGFloat duration = .3;
//        
//        [UIView animateWithDuration:duration animations:^{
//        
//            self.transform = CGAffineTransformTranslate(self.transform, 0, -self.keyboardOffset);
//            self.IschangekeyBoard = YES;
//
//        }];
//    } ;
//}

- (NSString *)convertStringToInt:(NSString* )hexString {
    NSString *str = @"";
    
    //  长度 - 1  因为最后一个字符是 '>' 不要的
    for(int i = 0;i < [hexString length];i ++)
        
    {
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        
        //        碰到空格 置为空
        if (hex_char1 == ' ') {
            hex_char1 = [hexString characterAtIndex:1 + i];
            //            NSLog(@"碰到一个空格");
            i++;
        }
        
        int int_ch1;
        
        if(hex_char1 >= '0' && hex_char1 <='9')
            
            int_ch1 = (hex_char1-48);   //// 0 的Ascll - 48
        
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            
            int_ch1 = (hex_char1-55); //// A 的Ascll - 65
        
        else
            
            int_ch1 = (hex_char1-87); //// a 的Ascll - 97
        
        if (int_ch1 > 9) {
            str = [NSString stringWithFormat:@"%@%d",str,int_ch1];
        } else {
            str = [NSString stringWithFormat:@"%@0%d",str,int_ch1];
        }
        
        
        
    }
    return str;
}


@end
